/*
 * spi_master_driver.h
 *
 *  Created on: 28 nov. 2013
 *      Author: crma
 */

#ifndef SPI_MASTER_DRIVER_H_
#define SPI_MASTER_DRIVER_H_

#include "spi/spi_master.h"
#include "spi/spi_frame.h"


typedef enum{
  spi_driver_fsm_idle,
  spi_driver_fsm_wait_host_ready,
  spi_driver_fsm_check_host_answer,
  spi_driver_fsm_max_state
}spi_driver_fsm_t;

typedef enum{
  spi_driver_result_ack_recvd,
  spi_driver_result_nack_recvd,
  spi_driver_result_timeout,
  spi_driver_result_max
}spi_driver_result_t;


typedef struct{
  spi_driver_fsm_t  state;          // state according to FSM
  uint32_t          tx_failures;    // number of transmission failures
  spi_frame_t       *msg;           // message in transmission
  uint8_t           retries;        // keeps track of the number of SPI Tx attempts
  uint32_t          timeout;        // set to the next timeout in milliseconds
}spi_driver_t;

extern bool spi_driver_init(spi_driver_t *spi_driver);
extern bool spi_driver_send_message(spi_driver_t *spi_driver, spi_frame_t *msg);
extern void spi_driver_log_error_set(spi_driver_t *spi_driver);
extern bool spi_driver_fsm_run(spi_driver_t *spi_driver);

#endif /* SPI_MASTER_DRIVER_H_ */
